﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Direct.Server.Modules.SysAdmin.Controls;
using Direct.Web.Mvc.Controls2;
using Direct.Core;

namespace Direct.Server.Modules.SysAdmin.SystemInfo.Views
{
    public class SysInfoModulesView : SysAdminMasterPage
    {
        private UiTreeViewItem CreateDefaultNode(string prefix)
        {
            var result = new UiTreeViewItem();
            result.SetFontWeight(CssFontWeight.Bold);
            result.SetTextBinding("{0}QuickInfo".Templ(prefix));
            result.AddItem(new UiTreeViewItem().SetTextBinding("{0}ModuleName".Templ(prefix), "Name: {0}"))
                .AddItem(new UiTreeViewItem().SetTextBinding("{0}ModuleDescription".Templ(prefix), "Description: {0}"))
                .AddItem(new UiTreeViewItem().SetTextBinding("{0}LoadOrder".Templ(prefix), "Load order: {0}"))
                .AddItem(new UiTreeViewItem().SetTextBinding("{0}ModuleCategory".Templ(prefix), "Category: {0}"))
                .AddItem(new UiTreeViewItem().SetTextBinding("{0}ModulePriority".Templ(prefix), "Priority: {0}"))
                .AddItem(new UiTreeViewItem().SetTextBinding("{0}State".Templ(prefix), "State: {0}"))
                .AddItem(new UiTreeViewItem().SetTextBinding("{0}ModuleType".Templ(prefix), "Type: {0}"))
                .AddItem(new UiTreeViewItem().SetTextBinding("{0}Ref".Templ(prefix), "Ref: {0}"))
                .AddItem(new UiTreeViewItem().SetTextBinding("{0}LoadOrderIndex".Templ(prefix), "SortKey: {0}"));
            return result;
        }

        protected override void Init()
        {
            base.Init();
            var content = this.Content;
            content.Add(new UiTitle().SetText("Módulos de servidor").SetSubtitle("Exibindo os módulos de servidor carregados"));
            var cnt = content.New<UiWidgetContent>().SetMargin(20).SetPadding(20).New<UiContentContainer>();
            cnt.AddText("Listando os módulos que foram carregados pelo servidor. Caso um módulo não conste na lista módulos abaixo, provavelmente ocorreu um erro ao instanciar o tipo que define o módulo ou ao carregar o assembly ou uma de suas dependências. Neste caso consulte o log de inicialização do servidor para obter mais informações.");
            cnt.AddLine(2);
            cnt.AddText("Módulos atualmente carregados:", CssFontWeight.Bold);
            var itemTemplate = CreateDefaultNode("Module.");
            itemTemplate.AddItem(new UiTreeViewItem()
                .SetBinding(UiTreeViewItem.ItemsSourceProperty, "DependsOn")
                .SetRenderEnabledBinding("DependsOn.Length:not-empty")
                .SetText("Depende de")
                .SetTreeItemTemplate(CreateDefaultNode("")));

            itemTemplate.AddItem(new UiTreeViewItem()
                .SetBinding(UiTreeViewItem.ItemsSourceProperty, "Dependents")
                .SetRenderEnabledBinding("Dependents.Length:not-empty")
                .SetText("Dependem deste")
                .SetTreeItemTemplate(CreateDefaultNode("")));

            //montar um tree view com os módulos
            cnt.New<UiList>()
                .SetPadding(5)
                .SetBinding(UiList.ItemsSourceProperty, "@modules")
                .SetItemTemplate(itemTemplate);
        }
    }
}
